﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoAn_BumTruong_DesignPattern.Admin
{
    public partial class Module : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        /// <summary>
        /// Khoi tao Facade
        /// </summary>
        Facades modulefacades = new Facades();

        ModuleController moduleController = new ModuleController();
        
        /// <summary>
        /// Thực hiện load toàn bộ dữ liệu
        /// </summary>
        private void LoadData()
        {
            //RepeaterModule.DataSource = menufacades.ModuleListAll().OrderByDescending(md=>md.ModuleID);
            int howManyPage = 0;
            string moduleID = Request.QueryString["ModuleID"] ?? "";
            string page = Request.QueryString["Page"] ?? "1";
            string fistPageUrl = Link.ToMenuAdmin("1");
            string pageUrl = Link.ToMenuAdmin("{0}");
            RepeaterModule.DataSource = modulefacades.PagingModule(page, out howManyPage).OrderByDescending(md => md.ModuleID);
            RepeaterModule.DataBind();

            PagesButton.ShowPage(int.Parse(page), howManyPage, fistPageUrl, pageUrl, true);
        }

        protected override void OnInitComplete(EventArgs e)
        {
            base.OnInitComplete(e);
            RepeaterModule.ItemCommand += RepeaterModule_ItemCommand;

            //Load position lên dropdownlist
            RepeaterModule.ItemDataBound += RepeaterModule_ItemDataBound;

            btnUpdateOrderNo.Click += btnUpdateOrderNo_Click;
        }

        void btnUpdateOrderNo_Click(object sender, EventArgs e)
        {
            string[] arr = Request.Form.AllKeys.Where(m => m.StartsWith("ord_")).ToArray();
            foreach (var item in arr)
            { 
                int orderNo = int.Parse(Request.Form[item]);
                int id = int.Parse(item.Split('_')[1]);
                moduleController.UpdateOrderNo(id, orderNo);
            }
            LoadData();
        }

        void RepeaterModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int index = e.Item.ItemIndex;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Module m = e.Item.DataItem as Module;
                DropDownList ddlPosition = (DropDownList)e.Item.FindControl("ddlPosition");
                int i = 0;
                foreach (string item in Enum.GetNames(typeof(ModulePositions)))
                {
                    ddlPosition.Items.Add(new ListItem(item, i + ""));
                    i += 1;
                }
                //ddlPosition.SelectedIndex = m.PositionDisplay;
            }
        }

        void RepeaterModule_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "UpdateActive")
            {
                string[] arg = e.CommandArgument.ToString().Split('_');
                ModuleController moduleController = new ModuleController();
                if (moduleController.UpdateActive(int.Parse(arg[0]), !Convert.ToBoolean(arg[1])))
                    LoadData();
            }
        }

        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool status = false;
            DropDownList ddlPosition = sender as DropDownList;
            int index = ((RepeaterItem)ddlPosition.NamingContainer).ItemIndex;
            string[] id = Request.Form.GetValues("module_id");
            string[] arr = id[index].Split('_');
            int selected = Convert.ToInt32(ddlPosition.SelectedValue);
            switch (selected)
            {
                case (int)ModulePositions.Left:
                    {
                        status = modulefacades.UpdatePositionModule(Convert.ToInt32(arr[0]), ((int)ModulePositions.Left));
                        break;
                    }
                case (int)ModulePositions.Right:
                    {
                        status = modulefacades.UpdatePositionModule(Convert.ToInt32(arr[0]), ((int)ModulePositions.Right));
                        break;
                    }
                case (int)ModulePositions.Top:
                    {
                        status = modulefacades.UpdatePositionModule(Convert.ToInt32(arr[0]), ((int)ModulePositions.Top));
                        break;
                    }
                case (int)ModulePositions.Bottom:
                    {
                        status = modulefacades.UpdatePositionModule(Convert.ToInt32(arr[0]), ((int)ModulePositions.Bottom));
                        break;
                    }
                default:
                    break;
            }
            LoadData();
            lblStatus.Text = status ? "Update successful!" : "Update failed!";
        }
    }
}